/**
 * 融资申请控制器 - 处理融资申请的创建、查询、评估等REST API接口
 *
 * 开发人员：马勇俐
 * 开发时间：2025-11-16 至 2025-11-20
 * 模块职责：提供融资申请相关的REST API接口，支持申请创建、评估、审批等融资业务流程
 *
 * 技术要点：
 * - RESTful API设计，遵循REST规范
 * - 多租户架构，企业级数据隔离
 * - 融资申请全生命周期管理
 * - AI评估结果集成展示
 * - 参数验证和异常处理机制
 *
 * 开发故事：负责融资申请控制器的开发，与融资服务层和前端融资页面深度配合，确保融资申请流程的完整性和用户体验。
 */
package com.pbl.controller;

import com.pbl.common.model.ApiResponse;
import com.pbl.entity.FinancingRequest;
import com.pbl.service.FinancingService;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/enterprises/{enterpriseId}/financing")
public class FinancingController {

    private final FinancingService financingService;

    /**
     * 构造融资控制器
     * @param financingService 融资服务层
     */
    public FinancingController(FinancingService financingService) {
        this.financingService = financingService;
    }

    @PostMapping
    public ApiResponse<FinancingRequest> create(@PathVariable Long enterpriseId,
                                                @RequestParam Long userId,
                                                @RequestParam @Min(1) BigDecimal requestedAmount,
                                                @RequestParam(required = false) String purpose) {
        return ApiResponse.success(financingService.create(enterpriseId, userId, requestedAmount, purpose));
    }

    @GetMapping
    public ApiResponse<List<FinancingRequest>> list(@PathVariable Long enterpriseId) {
        return ApiResponse.success(financingService.listByEnterprise(enterpriseId));
    }

    @PostMapping("/{id}/score")
    public ApiResponse<FinancingRequest> score(@PathVariable Long enterpriseId, @PathVariable Long id,
                                               @RequestParam BigDecimal score) {
        return ApiResponse.success(financingService.updateScore(id, score));
    }

    // 新增的增强API端点
    @GetMapping("/{id}/evaluate")
    public ApiResponse<Map<String, Object>> evaluate(@PathVariable Long enterpriseId, @PathVariable Long id) {
        return ApiResponse.success(financingService.evaluateFinancingRequest(id));
    }

    @GetMapping("/channels")
    public ApiResponse<List<Map<String, Object>>> getChannels(@PathVariable Long enterpriseId) {
        return ApiResponse.success(financingService.getFinancingChannels(enterpriseId));
    }

    @GetMapping("/health")
    public ApiResponse<Map<String, Object>> getFinancialHealth(@PathVariable Long enterpriseId) {
        return ApiResponse.success(financingService.getFinancialHealth(enterpriseId));
    }

    @GetMapping("/{id}/cost")
    public ApiResponse<Map<String, Object>> calculateCost(@PathVariable Long enterpriseId, 
                                                         @PathVariable Long id,
                                                         @RequestParam String channelType) {
        return ApiResponse.success(financingService.calculateFinancingCost(id, channelType));
    }

    @GetMapping("/recommendations")
    public ApiResponse<Map<String, Object>> getRecommendations(@PathVariable Long enterpriseId) {
        return ApiResponse.success(financingService.getFinancingRecommendations(enterpriseId));
    }
}


